package com.anhdt.dao;

import com.anhdt.utils.VUtils;
import com.anhdt.vo.UserVO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class UserDAO extends BaseDAO {

    private static UserDAO userDAO;

    private UserDAO() {
    }

    public static UserDAO getInstance() {
        if (userDAO == null) {
            userDAO = new UserDAO();
        }
        return userDAO;
    }

    public UserVO getUser(String userName, String password) throws SQLException {
        UserVO user = new UserVO();
        String sql = "SELECT * FROM user WHERE usename = ?  and password = ?";
        try (Connection conn = connect.getConnnection(); PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, userName);
            stmt.setString(2, password);
            try (ResultSet rs = stmt.executeQuery()) {
                if(rs.next()){
                    user.setFirstName(rs.getString("first_name"));
                    user.setLastName(rs.getString("last_name"));
                    VUtils.dateToString(new Date(), "dd/mm/yyyy");
                    user.setUserName(userName);
                    return user;
                }
            }
        }
        return null;
    }

}
